package com.glela.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.glela.common.util.DateUtil;
import com.glela.goods.mapper.BrandMapper;
import com.glela.goods.mapper.GoodsMapper;
import com.glela.goods.mapper.GoodsNavigateMapper;
import com.glela.goods.mapper.MarketingActivitiesDetailMapper;
import com.glela.goods.mapper.MarketingActivitiesMainMapper;
import com.glela.goods.mapper.MarketingActivitiesMapper;
import com.glela.goods.model.Brand;
import com.glela.goods.model.Goods;
import com.glela.goods.model.GoodsNavigate;
import com.glela.goods.model.GoodsPrice;
import com.glela.goods.model.MarketingActivities;
import com.glela.goods.model.MarketingActivitiesDetail;
import com.glela.goods.model.MarketingActivitiesMain;
import com.glela.goods.model.response.MarketingActivitiesGoodsDetailResponse;
import com.glela.goods.service.GoodsService;
import com.glela.goods.service.MarketingActivitiesService;
import com.glela.platform.mapper.UserMapper;
import com.glela.platform.model.User;
import com.glela.platform.model.UserInfo;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.service.UserService;

@Service("marketingActivitiesService")
@Scope("singleton")
public class MarketingActivitiesServiceImpl implements MarketingActivitiesService {

	@Autowired
	private MarketingActivitiesMapper marketingActivitiesMapper;
	@Autowired
	private MarketingActivitiesMainMapper marketingActivitiesMainMapper;
	@Autowired
	private MarketingActivitiesDetailMapper marketingActivitiesDetailMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private BrandMapper brandMapper;
	@Autowired
	private GoodsNavigateMapper goodsNavigateMapper;
	@Autowired
	private BaseRestrictionsService baseRestrictionsService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private GoodsService goodsService;
	
	
	/**
	 * 首页数据
	 */
	@Override
	public List<MarketingActivities> selectFourMarketingActivitiesImages() {
		String nowTime = DateUtil.currentStringDate();
		List<MarketingActivities> maList = new ArrayList<MarketingActivities>();
		List<MarketingActivities> maListWithType1 = this.marketingActivitiesMapper.selectWithType1(nowTime);
		if (!CollectionUtils.isEmpty(maListWithType1)) {
			MarketingActivities maWithType1 = maListWithType1.get(0);
			maList.add(maWithType1);
			List<MarketingActivities> maListWithType2 = this.marketingActivitiesMapper.selectByPidWithType2(maWithType1.getId());
			if (!CollectionUtils.isEmpty(maListWithType2)) {
				if (maListWithType2.size() < 3) {
					maList.addAll(maListWithType2);
				} else {
					maList.add(maListWithType2.get(0));
					maList.add(maListWithType2.get(1));
					maList.add(maListWithType2.get(2));
				}
			} else {
				maList = new ArrayList<MarketingActivities>();
			}
		}
		return maList;
	}
	
	/**
	 * 活动页数据
	 */
	@Override
	public List<MarketingActivitiesMain> selectByMarketingActivitiesId(Integer marketingActivitiesId) {
		return this.marketingActivitiesMainMapper.selectByMarketingActivitiesId(marketingActivitiesId);
	}
	
	@Override
	public MarketingActivities selectById(Integer marketingActivitiesId) {
		return this.marketingActivitiesMapper.selectByPrimaryKey(marketingActivitiesId);
	}
	/**
	 * @Description: 获取USETYPE为2的活动信息列表（只取一个）
	 * @Author: zhangrenjie
	 * @Date: 2018/1/10 14:21
	 *
	 */
	@Override
	public List<MarketingActivities> queryMarketingActivitiesImages() {
		String nowTime = DateUtil.currentStringDate();
		List<MarketingActivities> maList = new ArrayList<MarketingActivities>();
		List<MarketingActivities> maListUseType2 = this.marketingActivitiesMapper.selectWithUseType2(nowTime);
		if (!CollectionUtils.isEmpty(maListUseType2)) {
			MarketingActivities maWithUseType2 = maListUseType2.get(0);
			maList.add(maWithUseType2);
			List<MarketingActivities> maGroup = this.marketingActivitiesMapper.selectByPidWithType2(maWithUseType2.getId());
			if (!CollectionUtils.isEmpty(maGroup)) {
				if (maGroup.size() < 3) {
					maList.addAll(maGroup);
				} else {
					maList.add(maGroup.get(0));
					maList.add(maGroup.get(1));
					maList.add(maGroup.get(2));
				}
			} /*else {
				maList = new ArrayList<MarketingActivities>();
			}*/
		}
		return maList;
	}
	/**
	 * @Description: 查询活动商品列表（新）
	 * @Author: zhangrenjie
	 * @Date: 2018/1/10 15:39
	 *
	 */
	@Override
	public List<MarketingActivitiesGoodsDetailResponse> selectActivitiesGoodsList(Integer storeUserId, Integer pId) {
		List<MarketingActivitiesGoodsDetailResponse> line = new ArrayList<MarketingActivitiesGoodsDetailResponse>();
		List<MarketingActivitiesDetail> madList = this.marketingActivitiesDetailMapper.selectByPid(pId);
		if (!CollectionUtils.isEmpty(madList)) {
			User user = userService.getUserById(Long.valueOf(storeUserId));
			List<Long> goodsIds = new ArrayList<Long>();
			for(MarketingActivitiesDetail temp : madList){
				goodsIds.add(Long.valueOf(temp.getGoodsId()));
			}
			Map<Long,GoodsPrice> goodsPriceMap = this.goodsService.getGoodsViewPriceForGoods(user, goodsIds);
			for(MarketingActivitiesDetail mad : madList){
				MarketingActivitiesGoodsDetailResponse gdr = new MarketingActivitiesGoodsDetailResponse();
				Goods goods = this.goodsMapper.selectGoodsByIdTwo(mad.getGoodsId());
				if (goods == null) {
					continue;
				}
				gdr.setGoodKeyWords(mad.getKeyWords()==null?"":mad.getKeyWords());
				gdr.setGoodId(goods.getId());
				String brandNameCn = "";
				Brand brand = this.brandMapper.selectBrandById(goods.getBrandId());
				// 品牌为空返回更多品牌
				if (brand == null) {
					brand = this.brandMapper.selectBrandById(85);
				}
				if (brand != null && !StringUtils.isEmpty(brand.getNameCn())) {
					brandNameCn = brand.getNameCn();
				}
				gdr.setGoodsName((StringUtils.isEmpty(brandNameCn) ? "" : "[" + brandNameCn + "]")+goods.getName());

				String goodNavigateImage = mad.getImgUrl()==null? "": mad.getImgUrl();
				if (StringUtils.isBlank(mad.getImgUrl())) {
					List<GoodsNavigate> goodsNavigateList = this.goodsNavigateMapper.selectGoodsNavigateByGoodId(goods.getId());
					if (!CollectionUtils.isEmpty(goodsNavigateList)) {
						goodNavigateImage = goodsNavigateList.get(0).getNavigateImage();
					}
				}
				gdr.setGoodNavigateImage(goodNavigateImage);
				GoodsPrice goodsPrice = goodsPriceMap.get(goods.getId())==null?this.goodsService.getGoodsViewPriceForGoods(user, goods.getId()):goodsPriceMap.get(goods.getId());
				String marketPirce = "0";
				String salePrice = "0";
				String commission = "0";
				if (goodsPrice != null) {
					marketPirce = goodsPrice.getMarketPirce();
					salePrice = goodsPrice.getSalePrice();
					commission = goodsPrice.getCommission();
				}
				gdr.setMarketPirce(marketPirce);
				gdr.setSalePrice(salePrice);
				gdr.setCommission(commission);
				line.add(gdr);
			}
		}
		return line;
	}
	/**
	 * @Description: 根据活动ID查询子活动列表（目前限制一个活动仅对应一个子活动ID）
	 * @Author: zhangrenjie
	 * @Date: 2018/1/11 16:01
	 *
	 */
	@Override
	public List<MarketingActivities> selectActivitiesByPid(Integer pId) {
		return this.marketingActivitiesMapper.selectActivitiesByPid(pId);
	}

	/**
	 * 活动页明细数据
	 */
	@Override
	public List<MarketingActivitiesGoodsDetailResponse> selectByPid(Integer storeUserId,Integer pId) {
		List<MarketingActivitiesGoodsDetailResponse> line = new ArrayList<MarketingActivitiesGoodsDetailResponse>();
		List<MarketingActivitiesDetail> madList = this.marketingActivitiesDetailMapper.selectByPid(pId);
		if (!CollectionUtils.isEmpty(madList)) {
			// 批量查询价格信息
			User user = this.userMapper.selectByPrimaryKey(Long.valueOf(storeUserId));
			List<Long> goodsIds = new ArrayList<Long>();
			for(MarketingActivitiesDetail temp : madList){
				goodsIds.add(Long.valueOf(temp.getGoodsId()));
			}
			Map<Long,GoodsPrice> goodsPriceMap = this.goodsService.getGoodsViewPriceForGoods(user, goodsIds);
			for(MarketingActivitiesDetail mad : madList){
				MarketingActivitiesGoodsDetailResponse gdr = new MarketingActivitiesGoodsDetailResponse();
				Goods goods = this.goodsMapper.selectGoodsByIdTwo(mad.getGoodsId());
				if (goods == null) {
					continue;
				}
				gdr.setGoodId(goods.getId());
				String brandNameCn = "";
				Brand brand = this.brandMapper.selectBrandById(goods.getBrandId());
				// 品牌为空返回更多品牌
				if (brand == null) {
					brand = this.brandMapper.selectBrandById(85);
				}
				if (brand != null && !StringUtils.isEmpty(brand.getNameCn())) {
					brandNameCn = brand.getNameCn();
				}
				gdr.setGoodsName((StringUtils.isEmpty(brandNameCn) ? "" : "[" + brandNameCn + "]")+goods.getName());
				List<GoodsNavigate> goodsNavigateList = this.goodsNavigateMapper.selectGoodsNavigateByGoodId(goods.getId());
				String goodNavigateImage = "";
				if (!CollectionUtils.isEmpty(goodsNavigateList)) {
					goodNavigateImage = goodsNavigateList.get(0).getNavigateImage();
				}
				gdr.setGoodNavigateImage(goodNavigateImage);
				GoodsPrice goodsPrice = goodsPriceMap.get(goods.getId())==null?this.goodsService.getGoodsViewPriceForGoods(user, goods.getId()):goodsPriceMap.get(goods.getId());
				String marketPirce = "0";
				String salePrice = "0";
				String commission = "0";
				if (goodsPrice != null) {
					marketPirce = goodsPrice.getMarketPirce();
					salePrice = goodsPrice.getSalePrice();
					commission = goodsPrice.getCommission();
				}
				gdr.setMarketPirce(marketPirce);
				gdr.setSalePrice(salePrice);
				gdr.setCommission(commission);
				line.add(gdr);
			}
		}
		return line;
	}
	
}
